package com.lanou.renrenxin.activity;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.PopupWindow;

import com.lanou.renrenxin.R;
import com.lanou.renrenxin.tools.InstantCall;

/**
 * Created by zouhui on 16/3/19.
 */
public class SelectCallPopupWindow extends PopupWindow implements View.OnClickListener {
    private Button callVideo, callVoice, callCancel;
    private View mMenuView;
    private String username;
    private final InstantCall instantCall;

    public SelectCallPopupWindow(Activity context, String username) {
        super(context);
        this.username = username;
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.popupwindow_instan_call, null);
        callVideo = (Button) mMenuView.findViewById(R.id.btn_popupwindow_instan_call_video);
        callVoice = (Button) mMenuView.findViewById(R.id.btn_popupwindow_instan_call_voice);
        callCancel = (Button) mMenuView.findViewById(R.id.btn_popupwindow_instan_call_cancel);
        //取消按钮
        callCancel.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                //销毁弹出框
                dismiss();
            }
        });
        //设置按钮监听
        callVideo.setOnClickListener(this);
        callVoice.setOnClickListener(this);
        //设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(LayoutParams.FILL_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(LayoutParams.WRAP_CONTENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.AnimBottom);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        //mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        instantCall = new InstantCall(context,username);
        mMenuView.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {

                int height = mMenuView.findViewById(R.id.layout_popupwindow_instan_call).getTop();
                int y=(int) event.getY();
                if(event.getAction()==MotionEvent.ACTION_UP){
                    if(y<height){
                        dismiss();
                    }
                }
                return true;
            }
        });

    }


    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_popupwindow_instan_call_video:
                instantCall.startVideoCall();
                break;
            case R.id.btn_popupwindow_instan_call_voice:
                instantCall.startVoiceCall();
                break;
            case R.id.btn_popupwindow_instan_call_cancel:
                dismiss();
                break;
            default:
                break;
        }
        dismiss();
    }
}
